package com.hanxiaozhang.redis;

import com.hanxiaozhang.Configuration;
import com.hanxiaozhang.constant.RedisModelEnum;
import com.hanxiaozhang.exception.DistributedLockException;
import com.hanxiaozhang.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.*;

/**
 * 〈一句话功能简述〉<br>
 * 〈初始化Redis客户端单例〉
 *
 * @author hanxinghua
 * @create 2022/9/6
 * @since 1.0.0
 */
@Slf4j
public class RedisClientSingleton {

    private static volatile RedissonClient client;

    private RedisClientSingleton() {
    }

    public static RedissonClient getInstance(Configuration config) {
        if (null == client) {
            synchronized (RedisClientSingleton.class) {
                if (null == client) {
                    client = initRedisClient(config);
                }
            }
        }
        return client;
    }

    /**
     * 初始化Redis客户端
     *
     * @param configuration
     * @return
     */
    private static RedissonClient initRedisClient(Configuration configuration) {

        Config config = new Config();
        String redisModel = configuration.getRedisModel();
        if (!RedisModelEnum.containsCode(redisModel)) {
            throw new DistributedLockException("获取redisModel的值失败!");
        }
        String redisAddress = configuration.getRedisAddress();

        if (RedisModelEnum.SINGLE.getCode().equals(redisModel)) {
            SingleServerConfig singleServerConfig = config.useSingleServer();

            if (StringUtil.isBlank(redisAddress)) {
                throw new DistributedLockException("获取redisAddress的值失败!");
            }
            singleServerConfig.setAddress(redisAddress);
            if (StringUtil.isNotBlank(configuration.getRedisPassword())) {
                singleServerConfig.setPassword(configuration.getRedisPassword());
            }
            Integer redisDb = configuration.getRedisDb();
            if (redisDb != null) {
                singleServerConfig.setDatabase(redisDb);
            }
        }

        if (RedisModelEnum.CLUSTER.getCode().equals(redisModel)) {
            ClusterServersConfig clusterServersConfig = config.useClusterServers();

            if (StringUtil.isBlank(redisAddress)) {
                throw new DistributedLockException("获取redisAddress的值失败!");
            }
            clusterServersConfig.addNodeAddress(redisAddress.split(","));
        }

        if (RedisModelEnum.MASTER_SLAVE.getCode().equals(redisModel)) {
            MasterSlaveServersConfig masterSlaveServersConfig = config.useMasterSlaveServers();

            String redisMasterAddress = configuration.getRedisMasterAddress();
            if (StringUtil.isBlank(redisMasterAddress)) {
                throw new DistributedLockException("获取redisMasterAddress的值失败!");
            }
            masterSlaveServersConfig.setMasterAddress(redisMasterAddress);

            String redisSlaveAddresses = configuration.getRedisSlaveAddresses();
            if (StringUtil.isBlank(redisSlaveAddresses)) {
                throw new DistributedLockException("获取redisSlaveAddresses的值失败!");
            }
            masterSlaveServersConfig.addSlaveAddress(redisSlaveAddresses.split(","));
        }

        if (RedisModelEnum.SENTINEL.getCode().equals(redisModel)) {
            SentinelServersConfig sentinelServersConfig = config.useSentinelServers();

            String redisMasterName = configuration.getRedisMasterName();
            if (StringUtil.isBlank(redisMasterName)) {
                throw new DistributedLockException("获取redisMasterName的值失败!");
            }
            sentinelServersConfig.setMasterName(redisMasterName);

            String redisSentinelAddress = configuration.getRedisSentinelAddress();
            if (StringUtil.isBlank(redisSentinelAddress)) {
                throw new DistributedLockException("获取redisSentinelAddress的值失败!");
            }
            sentinelServersConfig.addSentinelAddress(redisSentinelAddress.split(","));
        }


        return Redisson.create(config);
    }

}
